package com.icss.ah.email;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
  
/** 
 * 
 * QQ(mail.qq.com):POP3服务器（端口995）SMTP服务器（端口465或587）。 
 * 
 */
public class Demo1 { 
  
  /** 
   * @param args 
   * @throws MessagingException 
   */
  public static void main(String[] args) throws MessagingException { 
      
    String sendUserName = "csadchen@163.com"; 
    String sendPassword = "cs577087"; 
      
    Properties properties = new Properties(); 
    properties.setProperty("mail.smtp.auth", "true");//服务器需要认证 
    properties.setProperty("mail.transport.protocol", "smtp");//声明发送邮件使用的端口 
      
    Session session = Session.getInstance(properties); 
    session.setDebug(true);//同意在当前线程的控制台打印与服务器对话信息 
      
    Message message = new MimeMessage(session);//构建发送的信息 
    message.setRecipient(Message.RecipientType.TO, new InternetAddress("chensheng@chinasofti.com"));
    message.setText("你好，我是csadchen");//信息内容 
    message.setSubject("java邮件发送测试");
    message.setFrom(new InternetAddress(sendUserName));//发件人 
      
    Transport transport = session.getTransport(); 
    transport.connect("smtp.163.com", 25, sendUserName, sendPassword);//连接发件人使用发件的服务器 
    transport.sendMessage(message, new Address[]{new InternetAddress("chensheng@chinasofti.com")});//接受邮件 
    transport.close(); 
  } 
  
} 